package org.i1510.common.service.impl;


import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.i1510.common.service.ICxf4UserInfoService;
import org.i1510.common.service.CxfInterceptor.WsOutAuthHandler;
import org.junit.Test;

public class Cxf4WsTest {

	@Test
	public void ClinetWSTest(){
System.out.println("juint test run!");
        
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(ICxf4UserInfoService.class);
        factory.setAddress("http://172.18.13.117:8080/wuyo/cxf/iCxf4UserInfo?wsdl");
        
        ICxf4UserInfoService webServiceSample = (ICxf4UserInfoService) factory.create();
        
        ClientProxy proxy = (ClientProxy) Proxy.getInvocationHandler(webServiceSample);
        Client client = proxy.getClient();
        
        Map<String,Object> outProps = new HashMap<String,Object>();
        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, "admin");
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, WsOutAuthHandler.class.getName());
        client.getOutInterceptors().add(new SAAJOutInterceptor());
        client.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
        
        Map<String, Object> inProps = new HashMap<String, Object>();
        inProps.put("action", "UsernameToken");
        inProps.put("passwordType", "PasswordText");
        inProps.put("passwordCallbackClass", "cn.org.coral.biz.examples.webservice.handler.WsOutAuthHandler");
        client.getInInterceptors().add(new SAAJInInterceptor());
        client.getInInterceptors().add(new WSS4JInInterceptor(inProps));
        
        System.out.println(webServiceSample.checkUserInfo("我是你的","是123"));
	}
}
